import axios from "axios";
import { ElMessage } from "element-plus";
import store from "@/store";
import { getToken, removeToken } from "@/utils/auth";
import router from "@/router";

const service = axios.create({
  baseURL: "/admin/", // url = base url + request url
  timeout: 10000, // request timeout
});

// request interceptor
service.interceptors.request.use(
  (config) => {
    // do something before request is sent
    if (store.getters.token) {
      config.headers["Authorization"] = "Bearer " + getToken();
    }

    config.headers["Accept"] = "application/json";
    // if (['/api/upload'].includes(config.url)) {
    //   config.headers['Content-Type'] = 'multi-part/form-data'
    // } else {
    //   config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    //   if (['post', 'patch'].includes(config.method)) {
    //     config.data = Qs.stringify(config.data)
    //   }
    // }

    return config;
  },
  (error) => {
    // do something with request error
    return Promise.reject(error);
  }
);

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  (response) => {
    const res = response.data;
    // if the custom code is not 20000, it is judged as an error.
    if (res.code === 1) {
      ElMessage({
        message: res.message || "Error",
        type: "error",
        duration: 5 * 1000,
        showClose: true,
      });
      return Promise.reject(new Error(res.message || "Error"));
    } else {
      return res;
    }
  },
  (error) => {
    if (
      error.response &&
      error.response.status !== 400 &&
      error.response.status !== 401 &&
      error.response.status !== 409
    ) {
      ElMessage({
        message:
          (error.response &&
            error.response.data &&
            error.response.data.message) ||
          error.message,
        type: "error",
        duration: 5 * 1000,
        showClose: true,
      });
    }
    if (error.response && error.response.status === 401) {
      ElMessage({
        message: "登录已经失效!",
        type: "error",
        duration: 5 * 1000,
        showClose: true,
      });
      setTimeout(removeToken(), 2000);
      setTimeout(router.push("/login"), 1000);
    }
    if (error.response && error.response.status === 400) {
      ElMessage({
        message: error.response.data.message,
        type: "error",
        duration: 5 * 1000,
        showClose: true,
      });
    }
    return Promise.reject(error);
  }
);

export default service;
